using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;
namespace EntGuayasSLA.BL
{
    [Serializable]
    public class ProrrogaEa : BusinessBase<ProrrogaEa>
    {
        #region Business Methods

        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(p => p.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        public static readonly PropertyInfo<string> MonitoreoIdProperty = RegisterProperty<string>(c => c.MonitoreoId);
        public string MonitoreoId
        {
            get { return GetProperty(MonitoreoIdProperty); }
            set { SetProperty(MonitoreoIdProperty, value); }
        }
        public static readonly PropertyInfo<string> CumpIdProperty = RegisterProperty<string>(c => c.CumpId);
        public string CumpId
        {
            get { return GetProperty(CumpIdProperty); }
            set { SetProperty(CumpIdProperty, value); }
        }
        public static readonly PropertyInfo<string> FaseIdProperty = RegisterProperty<string>(c => c.FaseId);
        public string FaseId
        {
            get { return GetProperty(FaseIdProperty); }
            set { SetProperty(FaseIdProperty, value); }
        }
        public static readonly PropertyInfo<DateTime> FechaInicialProperty = RegisterProperty<DateTime>(c => c.FechaInicial);
        public DateTime FechaInicial
        {
            get { return GetProperty(FechaInicialProperty); }
            set { SetProperty(FechaInicialProperty, value); }
        }

       public static readonly PropertyInfo<DateTime> FechaFinalProperty = RegisterProperty<DateTime>(c => c.FechaFinal);
        public DateTime FechaFinal
        {
            get { return GetProperty(FechaFinalProperty); }
            set { SetProperty(FechaFinalProperty, value); }
        }

        public static readonly PropertyInfo<string> MotivoProperty = RegisterProperty<string>(c => c.Motivo);
        public string Motivo
        {
            get { return GetProperty(MotivoProperty); }
            set { SetProperty(MotivoProperty, value); }
        }

        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            base.AddBusinessRules();

            //BusinessRules.AddRule(new Rule(IdProperty));
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion

        #region Factory Methods

        public static ProrrogaEl NewEditableRoot()
        {
            return DataPortal.Create<ProrrogaEl>();
        }

        public static ProrrogaEl GetEditableRoot(int id)
        {
            return DataPortal.Fetch<ProrrogaEl>(id);
        }

        public static void DeleteEditableRoot(int id)
        {
            DataPortal.Delete<ProrrogaEl>(id);
        }

        private ProrrogaEa()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(int criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_pea_id", ReadProperty(IdProperty)) };
            DataSet datos = accesoDatos.EjecutarDataSet("OBT_PRORROGA_EA", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0][0].ToString()));
                LoadProperty(MonitoreoIdProperty, datos.Tables[0].Rows[0][1].ToString());
                LoadProperty(CumpIdProperty, datos.Tables[0].Rows[0][2].ToString());
                LoadProperty(FaseIdProperty, datos.Tables[0].Rows[0][3].ToString());
                LoadProperty(FechaInicialProperty,Convert.ToDateTime(datos.Tables[0].Rows[0][4].ToString()));
                LoadProperty(FechaFinalProperty ,Convert.ToDateTime(datos.Tables[0].Rows[0][5].ToString()));
                LoadProperty(MotivoProperty, datos.Tables[0].Rows[0][6].ToString());
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
           
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_pea_id", ReadProperty(IdProperty)),
                new Parametro("ec_mon_id", ReadProperty(MonitoreoIdProperty)),
                new Parametro("ec_cpm_id", ReadProperty(CumpIdProperty)),
                new Parametro("ec_fac_id", ReadProperty(FaseIdProperty)),
                new Parametro("ef_pea_fecha_inicial",ReadProperty(FechaInicialProperty)),
                new Parametro("ef_pea_fecha_final", ReadProperty(FechaFinalProperty)),
                new Parametro("ec_pea_motivo", ReadProperty(MotivoProperty))
            };
            accesoDatos.EjecutarQuery("INS_PRORROGA_EA", parametros);
            
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
          
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_pea_id", ReadProperty(IdProperty)),
                new Parametro("ec_mon_id", ReadProperty(MonitoreoIdProperty)),
                new Parametro("ec_cpm_id", ReadProperty(CumpIdProperty)),
                new Parametro("ec_fac_id", ReadProperty(FaseIdProperty)),
                new Parametro("ef_pea_fecha_inicial",ReadProperty(FechaInicialProperty)),
                new Parametro("ef_pea_fecha_final", ReadProperty(FechaFinalProperty)),
                new Parametro("ec_pea_motivo", ReadProperty(MotivoProperty))
            };
            accesoDatos.EjecutarQuery("ACT_PRORROGA_EA", parametros);
      
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(this.Id);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_pea_id", ReadProperty(IdProperty))
            };
            accesoDatos.EjecutarQuery("ELI_PRORROGA_EA", parametros);
      
        }

        #endregion
    }
}
